package tools;

import InterfaceMahasiswa.FormMengisiPresensi;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import de.humatic.dsj.*;
import de.humatic.dsj.rc.RendererControls;
import InterfaceStaff.*;

public class QRReader implements java.beans.PropertyChangeListener, ActionListener {

    private DSCapture graph;
    private String kode = "";
   
    public QRReader() {
    }

    public void memindaiQRCode() {

        javax.swing.JFrame f = new javax.swing.JFrame("Scan QR Code Disini");
        f.setLocation(625, 0);
        f.setSize(400, 400);

        DSFilterInfo[][] dsi = DSCapture.queryDevices();

        graph = new DSCapture(DSFiltergraph.DD7, dsi[0][0], false, DSFilterInfo.doNotRender(), this);

        f.add(java.awt.BorderLayout.CENTER, graph.asComponent());

        f.pack();

        f.setVisible(true);

        f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Timer timer = new Timer(300, this);
        timer.start();
    }

    public void propertyChange(java.beans.PropertyChangeEvent pe) {
        switch (DSJUtils.getEventType(pe)) {
        }
    }

    public static void main(String[] args) {
        new QRReader().memindaiQRCode();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        BufferedImage bi = graph.getImage();
        List<ZBar.Symbol> ls = ZBar.scan(bi);
        if (ls.size() <= 0) {
            return;
        }

        BufferedImage overlay = new BufferedImage(graph.getWidth(), graph.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = overlay.createGraphics();
        g2d.setBackground(new Color(0, 0, 0, 0));
        g2d.setColor(Color.GREEN);
        int y0 = 0;

        for (ZBar.Symbol s : ls) {
            if (!kode.matches(s.data) && s.data.contains("10")) {
                System.out.println(s.data);
                try {
                    Presensi.getKoneksi().cekStatus1(s.data);
                } catch (SQLException ex) {
                    Logger.getLogger(QRReader.class.getName()).log(Level.SEVERE, null, ex);
//                    JOptionPane.showMessageDialog(null, "Anda Sudah Presensi");
                }
                FormMengisiPresensi.tampilNim(s.data);
                kode = s.data;
                g2d.drawPolygon(s.xs, s.ys, s.xs.length);
                g2d.drawString(s.data, 0, y0 += 20);
            } 
        }

        g2d.dispose();

        RendererControls rc = graph.getRendererControls();
        rc.setOverlayImage(overlay, null, Color.black, 1);
    }

    public String getKode() {
        return kode;
    }

    /**
     * @param kode the kode to set
     */
    public void setKode(String kode) {
        this.kode = kode;
    }
}
